//
// Created by 崔世强 on 2018/4/19.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift

class SessionDao {
    
    static func getSession(_ sessionServerId: Int, chatType: ChatType) throws -> Session? {
        let db = SQLiteHelper.getDb()
        let returnSession: Session? = try db.getObject(on: Session.CodingKeys.all, fromTable: Session.table, where: Session.CodingKeys.sessionServerId == sessionServerId && Session.CodingKeys.chatType == chatType)
        return returnSession
    }
    
    static func save(_ session: Session) throws {
        let db = SQLiteHelper.getDb()
        try db.insert(session, intoTable: Session.table)
    }
    static func getRecentSessions() throws -> [Session] {
        var returnSession = [Session]()
        if !AppStatusManager.instance.userLoginStatus.0 {
            return returnSession
        }
        let db = Database(at: AppStatusManager.instance.urlForUserDB ?? URL(fileURLWithPath: ""))
        returnSession = try db.getObjects(on: Session.CodingKeys.all,
                                          fromTable: Session.table,
                                          where: Session.CodingKeys.deleted == false,
                                          orderBy: [Session.CodingKeys.updatedDatetime.order(.descending)])
        return returnSession
    }
    
    //获取单聊和群聊的 所有 最近会话
    static func getAllRecentSessions() throws -> [Session] {
        let db = SQLiteHelper.getDb()
        let returnSession:[Session] = try db.getObjects(on: Session.CodingKeys.all,
                                          fromTable: Session.table,
                                          where: Session.CodingKeys.deleted == false && Session.CodingKeys.chatType != ChatType.service_platform,
                                          orderBy:[Session.CodingKeys.seq.asOrder(),Session.CodingKeys.top.order(.descending),Session.CodingKeys.updatedDatetime.order(.descending)])
        return returnSession
    }
    
    //根据  chatType 获取最近会话
    static func getRecentSessions(chatType:ChatType) throws -> [Session] {
        var returnSession = [Session]()
        let db = SQLiteHelper.getDb()
        returnSession = try db.getObjects(on: Session.CodingKeys.all,
                                          fromTable: Session.table,
                                          where: Session.CodingKeys.deleted == false && Session.CodingKeys.chatType == chatType,
                                          orderBy: [Session.CodingKeys.seq.asOrder(),Session.CodingKeys.top.order(.descending),Session.CodingKeys.updatedDatetime.order(.descending)])
        return returnSession
    }
    
    class func delete(_ sessionServerId: Int) throws {
        let db = SQLiteHelper.getDb()
        let row = Session()
        row.sessionServerId = sessionServerId
        row.deleted = 1
        try db.update(table: Session.table, on: Session.CodingKeys.deleted, with: row,where:Session.CodingKeys.sessionServerId == sessionServerId)
    }
    
    class func realDelete(_ sessionServerId: Int,chatType: ChatType) throws {
        let db = SQLiteHelper.getDb()
        let row = Session()
        row.sessionServerId = sessionServerId
        row.deleted = 1
        try db.delete(fromTable: Session.table, where: Session.CodingKeys.sessionServerId == sessionServerId && Session.CodingKeys.chatType == chatType)
    }
    
    class func update(_ sessionServerId: Int,deleted:Bool) throws {
        let db = SQLiteHelper.getDb()
        let row = Session()
        row.sessionServerId = sessionServerId
        row.deleted = deleted ? 1 : 0
        try db.update(table: Session.table, on: Session.CodingKeys.deleted, with: row,where:Session.CodingKeys.sessionServerId == sessionServerId)
    }
    
    class func updateAllGroupDeleted() throws {
        let db = SQLiteHelper.getDb()
        let row = Session()
        row.chatType = .group
        row.deleted = 1
        try db.update(table: Session.table, on: Session.CodingKeys.deleted, with: row,where:Session.CodingKeys.chatType == ChatType.group)
    }
    
    static func updateTop(_ id: Int, top: Bool) throws {
        let db = SQLiteHelper.getDb()
        let row = Session()
        row.sessionServerId = id
        row.top = top
        try db.update(table: Session.table, on: Session.CodingKeys.top, with: row, where: Session.CodingKeys.sessionServerId == id)
    }
}
